<template>
  <editor v-model="content" :init="init" tag-name="div" />
</template>
<script>
  import tinymce from 'tinymce/tinymce'
  import Editor from '@tinymce/tinymce-vue'
  import 'tinymce/themes/silver/theme' // 引用主题文件
  import 'tinymce/icons/default' // 引用图标文件
  import 'tinymce/models/dom'
  // tinymce插件可按自己的需要进行导入
  // 更多插件参考：https://www.tiny.cloud/docs/plugins/
  import 'tinymce/plugins/advlist' // 高级列表
  import 'tinymce/plugins/anchor' //锚点
  import 'tinymce/plugins/autolink' //自动链接
  import 'tinymce/plugins/autoresize'
  import 'tinymce/plugins/autosave'
  import 'tinymce/plugins/charmap' // 特殊字符
  import 'tinymce/plugins/code' // 查看源码
  import 'tinymce/plugins/codesample' // 插入代码
  import 'tinymce/plugins/directionality'
  import 'tinymce/plugins/emoticons'
  import 'tinymce/plugins/fullscreen' //全屏
  import 'tinymce/plugins/help'
  import 'tinymce/plugins/image' // 插入上传图片插件
  import 'tinymce/plugins/importcss' //图片工具
  import 'tinymce/plugins/insertdatetime' //时间插入
  import 'tinymce/plugins/link'
  import 'tinymce/plugins/lists' // 列表插件
  import 'tinymce/plugins/media' // 插入视频插件
  import 'tinymce/plugins/nonbreaking'
  import 'tinymce/plugins/pagebreak' //分页
  import 'tinymce/plugins/preview' // 预览
  import 'tinymce/plugins/quickbars'
  import 'tinymce/plugins/save' // 保存
  import 'tinymce/plugins/searchreplace' //查询替换
  import 'tinymce/plugins/table' // 插入表格插件
  import 'tinymce/plugins/template' //插入模板
  import 'tinymce/plugins/visualblocks'
  import 'tinymce/plugins/visualchars'
  import 'tinymce/plugins/wordcount' // 字数统计插件
  import axios from 'axios'
  import store from '@/store'

  export default {
    components: {
      Editor,
    },
    props: {
      modelValue: {
        type: String,
        default: '',
      },
    },
    emits: ['update:modelValue'],
    data() {
      return {
        content: this.modelValue,
        tinymceId: this.id,
        init: {
          // 语言设置
          language_url: '/tinymce/langs/zh-Hans.js', // 中文语言包路径
          language: 'zh-Hans', // 使用中文语言
          // 外观设置
          skin_url: '/tinymce/skins/ui/oxide', // 编辑器皮肤样式
          content_css: '/tinymce/skins/content/default/content.min.css', // 编辑器内容样式
          autoresize_bottom_margin: 50, // 自动调整大小的底部边距
          // 设置本地plugins中emojis的位置，用来获取表情,不用这个插件不用设置
          emoticons_database_url: '/tinymce/plugins/emoticons/js/emojis.js',
          //   max_height: 500, // 最大高度
          min_height: 450, // 最小高度
          // 工具栏设置
          toolbar_mode: 'sliding',
          menubar: 'file edit insert view format table', //菜单栏配置，设为false则隐藏，不配置则默认显示全部菜单，也可自定义配置--查看 http://tinymce.ax-z.cn/configure/editor-appearance.php --搜索“自定义菜单”
          menu: {
            // file: { title: '文件', items: 'newdocument' },
            edit: {
              title: '编辑',
              items: 'undo redo | cut copy paste pastetext | selectall',
            },
            insert: {
              title: '插入',
              items: 'link image media  |  hr',
            },
            view: {
              title: '查看',
              items: 'visualaid',
            },
            format: {
              title: '格式',
              items:
                'bold italic underline strikethrough superscript subscript | formats | removeformat',
            },
            table: {
              title: '表格',
              items: 'inserttable tableprops deletetable | cell row column',
            },
            tools: { title: '工具', items: 'spellchecker code' },
          },
          plugins:
            'wordcount visualchars visualblocks template searchreplace save quickbars preview pagebreak nonbreaking media insertdatetime importcss image help fullscreen directionality codesample code charmap link code table lists advlist anchor autolink autoresize autosave emoticons',
          // 启用的插件列表
          toolbar:
            'formats undo redo fontsizeselect fontselect ltr rtl searchreplace media | outdent indent aligncenter alignleft alignright alignjustify lineheight underline quicklink h2 h3 blockquote numlist bullist table removeformat forecolor backcolor bold italic strikethrough hr link preview fullscreen help emoticons',
          // 工具栏按钮配置
          // 样式设置
          content_style: 'p {margin: 5px 0; font-size: 14px}', // 内容样式
          fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px', // 字体大小格式
          // 字体设置
          font_formats:
            '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方= PingFang SC, Microsoft YaHei, sans- serif; 宋体 = simsun, serif; 仿宋体 = FangSong, serif; 黑体 = SimHei, sans - serif; Arial = arial, helvetica, sans - serif;Arial Black = arial black, avant garde;Book Antiqua = book antiqua, palatino; ', // 字体格式列表

          // 其他设置
          branding: false, // 隐藏编辑器品牌信息
          elementpath: false, // 隐藏元素路径提示
          resize: false, // 禁止编辑器改变大小
          statusbar: false, // 隐藏底部状态栏
          // 用于拖动图片到编辑框中
          //图片上传方法（注意！！：要使用 promise 对象中的 resolve 返回图片路径，否则会报错）
          images_upload_handler: (blobInfo) =>
            new Promise((resolve, reject) => {
              let formData = new FormData()
              formData.append('image', blobInfo.blob(), blobInfo.filename())
              formData.append('label', 'article')
              axios
                .post(
                  `http://localhost:8090/wapi/common/fileUpload`,
                  formData,
                  {
                    headers: {
                      'Content-Type': 'multipart/form-data',
                      accessToken: store.getters['user/accessToken'],
                    },
                  }
                )
                .then((res) => {
                  console.log(res)
                  if (res.data.code === 1) {
                    resolve(`${res.data.data}`)
                  } else {
                    console.log(res.data.msg)
                  }
                })
                .catch((error) => {
                  reject(error)
                })
            }),
        },
      }
    },
    computed: {},
    mounted() {
      // 初始化
      tinymce.init(this.init)
    },
    methods: {
      // props.modelValue改变以后更新文档
      watchProps(newVal) {
        this.content = newVal.modelValue
      },
      // content变化的时候更新modelValue
      watchContent(newVal) {
        this.$emit('update:modelValue', newVal)
      },
    },
    watch: {
      'props.modelValue': 'watchProps',
      content: 'watchContent',
    },
  }
</script>

<!-- 使用 Composition API  -->
<!-- <script setup>
  import tinymce from 'tinymce/tinymce'
  import Editor from '@tinymce/tinymce-vue'
  import { ref, watch, defineEmits, defineProps } from 'vue'
  import 'tinymce/themes/silver/theme' // 引用主题文件
  import 'tinymce/icons/default' // 引用图标文件
  import 'tinymce/models/dom'
  // tinymce插件可按自己的需要进行导入
  // 更多插件参考：https://www.tiny.cloud/docs/plugins/
  import 'tinymce/plugins/advlist'
  import 'tinymce/plugins/anchor'
  import 'tinymce/plugins/autolink'
  import 'tinymce/plugins/autoresize'
  import 'tinymce/plugins/autosave'
  import 'tinymce/plugins/charmap' // 特殊字符
  import 'tinymce/plugins/code' // 查看源码
  import 'tinymce/plugins/codesample' // 插入代码
  import 'tinymce/plugins/directionality'
  import 'tinymce/plugins/emoticons'
  import 'tinymce/plugins/fullscreen' //全屏
  import 'tinymce/plugins/help'
  import 'tinymce/plugins/image' // 插入上传图片插件
  import 'tinymce/plugins/importcss' //图片工具
  import 'tinymce/plugins/insertdatetime' //时间插入
  import 'tinymce/plugins/link'
  import 'tinymce/plugins/lists' // 列表插件
  import 'tinymce/plugins/media' // 插入视频插件
  import 'tinymce/plugins/nonbreaking'
  import 'tinymce/plugins/pagebreak' //分页
  import 'tinymce/plugins/preview' // 预览
  import 'tinymce/plugins/quickbars'
  import 'tinymce/plugins/save' // 保存
  import 'tinymce/plugins/searchreplace' //查询替换
  import 'tinymce/plugins/table' // 插入表格插件
  import 'tinymce/plugins/template' //插入模板
  import 'tinymce/plugins/visualblocks'
  import 'tinymce/plugins/visualchars'
  import 'tinymce/plugins/wordcount' // 字数统计插件

  // v-model
  const props = defineProps({
    modelValue: {
      type: String,
      default: '',
    },
  })
  const emit = defineEmits(['update:modelValue'])
  // 配置
  const init = {
    language_url: '/tinymce/langs/zh-Hans.js', // 中文语言包路径
    language: 'zh-Hans',
    skin_url: '/tinymce/skins/ui/oxide', // 编辑器皮肤样式
    content_css: '/tinymce/skins/content/default/content.min.css',
    menubar: false, // 隐藏菜单栏
    autoresize_bottom_margin: 50,
    max_height: 500,
    min_height: 450,
    // height: 320,
    toolbar_mode: 'none',
    plugins:
      'wordcount visualchars visualblocks template searchreplace save quickbars preview pagebreak nonbreaking media insertdatetime importcss image help fullscreen directionality codesample code charmap link code table lists advlist anchor autolink autoresize autosave',
    toolbar:
      'formats undo redo fontsizeselect fontselect ltr rtl searchreplace media | outdent indent aligncenter alignleft alignright alignjustify lineheight underline quicklink h2 h3 blockquote numlist bullist table removeformat forecolor backcolor bold italic strikethrough hr link preview fullscreen help ',
    content_style: 'p {margin: 5px 0; font-size: 14px}',
    fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px',
    font_formats:
      '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方= PingFang SC, Microsoft YaHei, sans- serif; 宋体 = simsun, serif; 仿宋体 = FangSong, serif; 黑体 = SimHei, sans - serif; Arial = arial, helvetica, sans - serif;Arial Black = arial black, avant garde;Book Antiqua = book antiqua, palatino; ',
    branding: false,
    elementpath: false,
    resize: false, // 禁止改变大小
    statusbar: false, // 隐藏底部状态栏
  }

  tinymce.init // 初始化
  const content = ref(props.modelValue)
  watch(props, (newVal) => (content.value = newVal.modelValue))
  watch(content, (newVal) => emit('update:modelValue', newVal))
</script> -->

<style scoped>
  .tox-tinymce-aux {
    z-index: 9999 !important;
  }
</style>
